package co.com.inventarios.persistencia;

import co.com.inventarios.modelo.Mngtdc;
import co.com.inventarios.modelo.MngtdcId;

import java.util.List;
import java.util.logging.Level;
import javax.persistence.EntityManager;
import javax.persistence.Query;

/**
 * A data access object (DAO) providing persistence and search support for
 * Mngtdc entities. Transaction control of the save(), update() and delete()
 * operations must be handled externally by senders of these methods or must be
 * manually added to each of these methods for data to be persisted to the JPA
 * datastore.
 * 
 * @see co.com.inventarios.modelo.Mngtdc
 * @author MyEclipse Persistence Tools
 */

public class MngtdcDAO implements IMngtdcDAO {
	// property constants
	public static final String TDCNOMBRE = "tdcnombre";
	public static final String TDCNUMERAR = "tdcnumerar";
	public static final String TDCNUMERO = "tdcnumero";
	public static final String TDCEXTERNO = "tdcexterno";
	public static final String TDCBODEGAS = "tdcbodegas";
	public static final String TDCAGREGA = "tdcagrega";
	public static final String TDCEDITA = "tdcedita";
	public static final String TDCELIMINA = "tdcelimina";
	public static final String TDCREABRE = "tdcreabre";
	public static final String TDCCONSULT = "tdcconsult";
	public static final String TDCIMPRIME = "tdcimprime";
	public static final String TDCPRECATA = "tdcprecata";
	public static final String TDCACTIVO = "tdcactivo";

	private EntityManager getEntityManager() {
		return EntityManagerHelper.getEntityManager();
	}

	/**
	 * Perform an initial save of a previously unsaved Mngtdc entity. All
	 * subsequent persist actions of this entity should use the #update()
	 * method. This operation must be performed within the a database
	 * transaction context for the entity's data to be permanently saved to the
	 * persistence store, i.e., database. This method uses the
	 * {@link javax.persistence.EntityManager#persist(Object) EntityManager#persist}
	 * operation.
	 * 
	 * <pre>
	 * EntityManagerHelper.beginTransaction();
	 * MngtdcDAO.save(entity);
	 * EntityManagerHelper.commit();
	 * </pre>
	 * 
	 * @param entity
	 *            Mngtdc entity to persist
	 * @throws RuntimeException
	 *             when the operation fails
	 */
	public void save(Mngtdc entity) {
		EntityManagerHelper.log("saving Mngtdc instance", Level.INFO, null);
		try {
			getEntityManager().persist(entity);
			EntityManagerHelper.log("save successful", Level.INFO, null);
		} catch (RuntimeException re) {
			EntityManagerHelper.log("save failed", Level.SEVERE, re);
			throw re;
		}
	}

	/**
	 * Delete a persistent Mngtdc entity. This operation must be performed
	 * within the a database transaction context for the entity's data to be
	 * permanently deleted from the persistence store, i.e., database. This
	 * method uses the
	 * {@link javax.persistence.EntityManager#remove(Object) EntityManager#delete}
	 * operation.
	 * 
	 * <pre>
	 * EntityManagerHelper.beginTransaction();
	 * MngtdcDAO.delete(entity);
	 * EntityManagerHelper.commit();
	 * entity = null;
	 * </pre>
	 * 
	 * @param entity
	 *            Mngtdc entity to delete
	 * @throws RuntimeException
	 *             when the operation fails
	 */
	public void delete(Mngtdc entity) {
		EntityManagerHelper.log("deleting Mngtdc instance", Level.INFO, null);
		try {
			entity = getEntityManager().getReference(Mngtdc.class,
					entity.getId());
			getEntityManager().remove(entity);
			EntityManagerHelper.log("delete successful", Level.INFO, null);
		} catch (RuntimeException re) {
			EntityManagerHelper.log("delete failed", Level.SEVERE, re);
			throw re;
		}
	}

	/**
	 * Persist a previously saved Mngtdc entity and return it or a copy of it to
	 * the sender. A copy of the Mngtdc entity parameter is returned when the
	 * JPA persistence mechanism has not previously been tracking the updated
	 * entity. This operation must be performed within the a database
	 * transaction context for the entity's data to be permanently saved to the
	 * persistence store, i.e., database. This method uses the
	 * {@link javax.persistence.EntityManager#merge(Object) EntityManager#merge}
	 * operation.
	 * 
	 * <pre>
	 * EntityManagerHelper.beginTransaction();
	 * entity = MngtdcDAO.update(entity);
	 * EntityManagerHelper.commit();
	 * </pre>
	 * 
	 * @param entity
	 *            Mngtdc entity to update
	 * @returns Mngtdc the persisted Mngtdc entity instance, may not be the same
	 * @throws RuntimeException
	 *             if the operation fails
	 */
	public Mngtdc update(Mngtdc entity) {
		EntityManagerHelper.log("updating Mngtdc instance", Level.INFO, null);
		try {
			Mngtdc result = getEntityManager().merge(entity);
			EntityManagerHelper.log("update successful", Level.INFO, null);
			return result;
		} catch (RuntimeException re) {
			EntityManagerHelper.log("update failed", Level.SEVERE, re);
			throw re;
		}
	}

	public Mngtdc findById(MngtdcId id) {
		EntityManagerHelper.log("finding Mngtdc instance with id: " + id,
				Level.INFO, null);
		try {
			getEntityManager().clear();
			Mngtdc instance = getEntityManager().find(Mngtdc.class, id);
			return instance;
		} catch (RuntimeException re) {
			EntityManagerHelper.log("find failed", Level.SEVERE, re);
			throw re;
		}
	}

	/**
	 * Find all Mngtdc entities with a specific property value.
	 * 
	 * @param propertyName
	 *            the name of the Mngtdc property to query
	 * @param value
	 *            the property value to match
	 * @return List<Mngtdc> found by query
	 */
	@SuppressWarnings("unchecked")
	public List<Mngtdc> findByProperty(String propertyName, final Object value) {
		EntityManagerHelper.log("finding Mngtdc instance with property: "
				+ propertyName + ", value: " + value, Level.INFO, null);
		try {
			getEntityManager().clear();
			final String queryString = "select model from Mngtdc model where model."
					+ propertyName + "= :propertyValue";
			Query query = getEntityManager().createQuery(queryString);
			query.setParameter("propertyValue", value);
			return query.getResultList();
		} catch (RuntimeException re) {
			EntityManagerHelper.log("find by property name failed",
					Level.SEVERE, re);
			throw re;
		}
	}

	public List<Mngtdc> findByTdcnombre(Object tdcnombre) {
		return findByProperty(TDCNOMBRE, tdcnombre);
	}

	public List<Mngtdc> findByTdcnumerar(Object tdcnumerar) {
		return findByProperty(TDCNUMERAR, tdcnumerar);
	}

	public List<Mngtdc> findByTdcnumero(Object tdcnumero) {
		return findByProperty(TDCNUMERO, tdcnumero);
	}

	public List<Mngtdc> findByTdcexterno(Object tdcexterno) {
		return findByProperty(TDCEXTERNO, tdcexterno);
	}

	public List<Mngtdc> findByTdcbodegas(Object tdcbodegas) {
		return findByProperty(TDCBODEGAS, tdcbodegas);
	}

	public List<Mngtdc> findByTdcagrega(Object tdcagrega) {
		return findByProperty(TDCAGREGA, tdcagrega);
	}

	public List<Mngtdc> findByTdcedita(Object tdcedita) {
		return findByProperty(TDCEDITA, tdcedita);
	}

	public List<Mngtdc> findByTdcelimina(Object tdcelimina) {
		return findByProperty(TDCELIMINA, tdcelimina);
	}

	public List<Mngtdc> findByTdcreabre(Object tdcreabre) {
		return findByProperty(TDCREABRE, tdcreabre);
	}

	public List<Mngtdc> findByTdcconsult(Object tdcconsult) {
		return findByProperty(TDCCONSULT, tdcconsult);
	}

	public List<Mngtdc> findByTdcimprime(Object tdcimprime) {
		return findByProperty(TDCIMPRIME, tdcimprime);
	}

	public List<Mngtdc> findByTdcprecata(Object tdcprecata) {
		return findByProperty(TDCPRECATA, tdcprecata);
	}

	public List<Mngtdc> findByTdcactivo(Object tdcactivo) {
		return findByProperty(TDCACTIVO, tdcactivo);
	}

	/**
	 * Find all Mngtdc entities.
	 * 
	 * @return List<Mngtdc> all Mngtdc entities
	 */
	@SuppressWarnings("unchecked")
	public List<Mngtdc> findAll() {
		EntityManagerHelper.log("finding all Mngtdc instances", Level.INFO,
				null);
		try {
			getEntityManager().clear();
			final String queryString = "select model from Mngtdc model";
			Query query = getEntityManager().createQuery(queryString);
			return query.getResultList();
		} catch (RuntimeException re) {
			EntityManagerHelper.log("find all failed", Level.SEVERE, re);
			throw re;
		}
	}

}